<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item :label="$t('购买时间')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="buyTime">
              <j-date :placeholder="$t('请选择购买时间')"  v-model="model.buyTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('订单类型')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderType">
              <j-dict-select-tag type="list" v-model="model.orderType" dictCode="bis_order.order_type"
                                 :placeholder="$t('请选择订单类型')" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('订单ID')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderId">
              <a-input v-model="model.orderId" :placeholder="$t('请输入订单ID')"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('商品名称')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="goodsName">
              <a-input v-model="model.goodsName" :placeholder="$t('请输入商品名称')"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('商品形态')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="form">
              <j-dict-select-tag type="list" v-model="model.form" dictCode="bis_order_goods.form"
                                 :placeholder="$t('请选择商品形态')" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('商品类别名称')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="goodsKindName">
              <a-input v-model="model.goodsKindName" :placeholder="$t('请输入商品类别名称')"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('成本单价')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cost">
              <a-input-number v-model="model.cost" :placeholder="$t('请输入成本单价')" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('销售单价')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price">
              <a-input-number v-model="model.price" :placeholder="$t('请输入销售单价')" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('数量')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amount">
              <a-input-number v-model="model.amount" :placeholder="$t('请输入数量')" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('销售金额')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totle">
              <a-input-number v-model="model.totle" :placeholder="$t('请输入销售金额')" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('状态（0 未取货；1 已取货；2 已退）')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="state">
              <a-input-number v-model="model.state" :placeholder="$t('请输入状态（0 未取货；1 已取货；2 已退）')" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('取货时间')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="takeTime">
              <j-date :placeholder="$t('请选择取货时间')"  v-model="model.takeTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('已取数量')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="takeNum">
              <a-input-number v-model="model.takeNum" :placeholder="$t('请输入已取数量')" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('是否有效')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="valid">
              <a-input-number v-model="model.valid" :placeholder="$t('请输入是否有效')" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'GoodsSalesForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           buyTime: [
              { required: true, message: this.$t('请输入购买时间')},
           ],
           orderType: [
              { required: true, message: this.$t('请输入订单类型')},
           ],
           orderId: [
              { required: true, message: this.$t('请输入订单ID')},
           ],
           goodsName: [
              { required: true, message: this.$t('请输入商品名称')},
           ],
           form: [
              { required: true, message: this.$t('请选择商品形态')},
           ],
           goodsKindName: [
              { required: true, message: this.$t('请输入商品类别名称')},
           ],
           cost: [
              { required: true, message: this.$t('请输入成本单价')},
           ],
           price: [
              { required: true, message: this.$t('请输入销售单价')},
           ],
           amount: [
              { required: true, message: this.$t('请输入数量')},
           ],
           totle: [
              { required: true, message: this.$t('请输入销售金额')},
           ],
           state: [
              { required: true, message: this.$t('请输入状态（0 未取货；1 已取货；2 已退）')},
           ],
           takeNum: [
              { required: true, message: this.$t('请输入已取数量')},
           ],
           valid: [
              { required: true, message: this.$t('请输入是否有效')},
           ],
        },
        url: {
          add: "/ordergoods/bisOrderGoods/add",
          edit: "/ordergoods/bisOrderGoods/edit",
          queryById: "/ordergoods/bisOrderGoods/queryBisOrderGoodsDetailByMainId"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>